package com.zjkj.mp.controller.userManage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zjkj.mp.service.user.UserService;
import com.zjkj.mp.utils.PaginatedResult;
import com.zjkj.mp.vo.user.MpUser;

/**
 * 
 * 用户列表控制器
 *
 * @author 陈晨
 * @history
 * <TABLE id="HistoryTable" border="1">
 * 	<TR><TD>时间</TD><TD>描述</TD><TD>作者</TD></TR>
 *	<TR><TD>2013-6-9</TD><TD>创建初始版本</TD><TD>陈晨</TD></TR>
 * </TABLE>
 */
@Controller
public class UserListController {
	private static final int PAGE_SIZE = 10;
	@Resource(name = "userServiceImpl")
	private UserService userService;
	
 
	/**
	 * 显示用户列表页面
	 */
	@RequestMapping("/userList.do")
	public String userList(HttpServletRequest req){
		String pageNoStr = req.getParameter("pageNo");
		int pageNo = 1;
		if(pageNoStr != null){
			pageNo = Integer.parseInt(pageNoStr);
		}
		PaginatedResult result = this.userService.getUserList(pageNo, PAGE_SIZE,null);
		
		req.setAttribute("result", result);		
		return "user/userList";
	}
	
	/**
	 * 删除用户
	 */
	@RequestMapping("deleteUser.do")
	public String deleteUser(HttpServletRequest req){
		String userId = req.getParameter("userId");
		if(userId != null){
			this.userService.disableUser(Integer.parseInt(userId));
		}
		return userList(req);
	} 
	
	/**
	 * 编辑用户信息
	 */
	@RequestMapping("editUser.do")
	public String editUser(HttpServletRequest req){
		String userIDStr = req.getParameter("userId");		
		MpUser user = this.userService.findUserById(Integer.parseInt(userIDStr));
				
		req.setAttribute("user", user);
		return "user/editUser";
	}
	
	/**
	 * 保存编辑的用户信息
	 */
	@RequestMapping("/doEditUser.do")
	public String doEditUser(HttpServletRequest req){
		//得到新建用户信息
		String realName = req.getParameter("realName");
		String mobile = req.getParameter("mobile");
		String phone = req.getParameter("phone");
		String password = req.getParameter("password");
		String birthday = req.getParameter("birthday");
		String unit = req.getParameter("unit");
		String address = req.getParameter("address");
		String sex = req.getParameter("sex");
		String note = req.getParameter("note");
		String email = req.getParameter("email");
		String userId = req.getParameter("id");
		String isAudit = req.getParameter("isAudit");
		String errorMsg = null;
		 
		//检查用户ID是否存在
		if(userId == null){
			errorMsg = "用户信息不存在，保存用户信息失败！";
			req.setAttribute("errorMsg", errorMsg);
			return userDetail(userId, req);
		}
		 
		//组装对象
		MpUser user  = this.userService.findUserById(Integer.parseInt(userId));
		if(user != null){
			//检查email是否存在
			if(!user.getEmail().equals(email) && this.userService.findByEmail(email) != null){
				errorMsg = "此邮箱已经被注册";
				req.setAttribute("errorMsg", errorMsg);
				return userDetail(userId, req);
			}
			
			user.setRealName(realName);
			user.setMobile(mobile);
			user.setPhone(phone);
			user.setPassword(password);
			user.setBirthDay(birthday);
			user.setUnit(unit);
			user.setAddress(address);
			user.setSex(sex);
			user.setNote(note);
			user.setEmail(email);			
			user.setAudit(isAudit.equals("true")?true:false);
			
			//开始更新
			this.userService.updateInfo(user);
		} 
		
		req.setAttribute("promptMsg", "信息修改成功！");
		return userDetail(user.getUserId() + "", req);
	} 
	
	/**
	 * 显示用户详细信息
	 */
	@RequestMapping("userDetail.do")
	public String userDetail(@RequestParam("userId")String userId,HttpServletRequest req){
		if(userId != null){
			MpUser user = this.userService.findUserById(Integer.parseInt(userId));
			req.setAttribute("user", user);
		}		
		return "user/userDetail";
	}
	
	/**
	 * 用户加入黑名单
	 */
	@RequestMapping("addToblackList.do")
	public String blackList(HttpServletRequest req){
		String userId = req.getParameter("userId");
		this.userService.addToBlackList(Integer.parseInt(userId));
		
		//返回列表
		return userList(req);
	}
}
